home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assemblers / cas.lha / data / kernel.s < prev    next >
Encoding:
Text File  |  1992-08-10  |  6.0 KB  |  282 lines

  1. include "8051fa.h"
  2. include "kernel.h"
  3. include "data.h"
  4.  
  5. extern code main
  6. seg data at 0
  7. RB0: ds 8 ;;; Register space.
  8.  
  9. seg data at 8
  10. /* Process stack. */
  11. HW:    ds 1 ;;; int **HW;
  12. Stack: ds 3 ;;; int *Stack[3];
  13.  
  14. /* Interrupt descriptor table. */
  15. global SP_IE0: ds 1
  16. global SP_RI:  ds 1
  17. global SP_TI:  ds 1
  18.  
  19. seg data
  20. SP_BASE: ds 2
  21.          ds 0x18
  22.  
  23. seg code at 0
  24. ajmp Start
  25.  
  26. at 0x03; mov R0, #SP_IE0; acall Resume; reti
  27. at 0x0b; acall Process; reti
  28.  
  29. DidTx: mov R0, #SP_TI; acall Resume; reti
  30. DidRx: mov R0, #SP_RI; acall Resume; reti
  31.  
  32. at 0x23
  33.    jbc TI, DidTx
  34.    clr RI
  35. sjmp DidRx
  36.  
  37. at 0x2b
  38.    jnb EXF2, DidTF2
  39.    jnb TF2, DidEXF2
  40. sjmp DidBoth
  41.  
  42. at 0x33
  43.    jb PCF, DequeueHigh
  44.    jnb CF, DequeueLow
  45. sjmp UpdateStatus
  46.  
  47. DidBoth:
  48.    mov A, RCAP2H
  49.    jb ACC.7, DidEXF2 ;;; This determines if TF2 or EXF2 was first.
  50. DidTF2:
  51.    clr TF2
  52.    acall Tick
  53. reti
  54. DidEXF2:
  55.    clr EXF2
  56.    djnz Sector0, 1f
  57.       mov Sector0, #SECTORS
  58.       mov TIME0 + 0, RCAP2L
  59.       mov TIME0 + 1, RCAP2H
  60.       mov TIME0 + 2, TIME + 0
  61.       mov TIME0 + 3, TIME + 1
  62.       setb TC0
  63.    1:
  64. reti
  65. EndSeg = $
  66.  
  67. ;;; Software to insert to handle 5 additional falling edge capture counters.
  68. ;;; This will resolve multiple EC interrupts in such a way that
  69. ;;; captures that occur before timer overflows are handled first.
  70. seg data at 0x20
  71. global CSTATUS: ds 1       ;;; High-priority interrupt queue.
  72. global PCF  bit CSTATUS.7
  73. global PCF0 bit CSTATUS.0
  74. global PCF1 bit CSTATUS.1
  75. global PCF2 bit CSTATUS.2
  76. global PCF3 bit CSTATUS.3
  77. global PCF4 bit CSTATUS.4
  78.  
  79. seg code at EndSeg
  80. UpdateStatus: ;;; Update CSTATUS 
  81.    setb PCF
  82. 1: jnb CCF0, 1f; mov A, CCAP0H; mov C, ACC.7; mov PCF0, C
  83. 1: jnb CCF1, 1f; mov A, CCAP1H; mov C, ACC.7; mov PCF1, C
  84. 1: jnb CCF2, 1f; mov A, CCAP2H; mov C, ACC.7; mov PCF2, C
  85. 1: jnb CCF3, 1f; mov A, CCAP3H; mov C, ACC.7; mov PCF3, C
  86. 1: jnb CCF4, 1f; mov A, CCAP4H; mov C, ACC.7; mov PCF4, C
  87. 1:
  88. DequeueHigh:        ;;; Dequeue high priority flags.
  89.    jbc PCF0, DoCCF0
  90.    jbc PCF1, DoCCF1
  91.    jbc PCF2, DoCCF2
  92.    jbc PCF3, DoCCF3
  93.    jbc PCF4, DoCCF4
  94. DoCF:
  95.    clr PCF
  96.    clr CF
  97.    acall CTick
  98. reti
  99. DequeueLow:         ;;; Dequeue low priority flags.
  100.    jbc CCF0, DidCCF0
  101.    jbc CCF1, DidCCF1
  102.    jbc CCF2, DidCCF2
  103.    jbc CCF3, DidCCF3
  104. DoCCF4:
  105.    clr CCF4
  106. DidCCF4:
  107.    djnz Sector5, 1f
  108.       mov Sector5, #SECTORS
  109.       mov (TIME5 + 0), CCAP4L
  110.       mov (TIME5 + 1), CCAP4H
  111.       mov (TIME5 + 2), (CTIME + 0)
  112.       mov (TIME5 + 3), (CTIME + 1)
  113.       setb TC5
  114.    1:
  115. reti
  116. DoCCF3:
  117.    clr CCF3
  118. DidCCF3:
  119.    djnz Sector4, 1f
  120.       mov Sector4, #SECTORS
  121.       mov (TIME4 + 0), CCAP3L
  122.       mov (TIME4 + 1), CCAP3H
  123.       mov (TIME4 + 2), (CTIME + 0)
  124.       mov (TIME4 + 3), (CTIME + 1)
  125.       setb TC4
  126.    1:
  127. reti
  128. DoCCF2:
  129.    clr CCF2
  130. DidCCF2:
  131.    djnz Sector3, 1f
  132.       mov Sector3, #SECTORS
  133.       mov (TIME3 + 0), CCAP2L
  134.       mov (TIME3 + 1), CCAP2H
  135.       mov (TIME3 + 2), (CTIME + 0)
  136.       mov (TIME3 + 3), (CTIME + 1)
  137.       setb TC3
  138.    1:
  139. reti
  140. DoCCF1:
  141.    clr CCF1
  142. DidCCF1:
  143.    djnz Sector2, 1f
  144.       mov Sector2, #SECTORS
  145.       mov (TIME2 + 0), CCAP1L
  146.       mov (TIME2 + 1), CCAP1H
  147.       mov (TIME2 + 2), (CTIME + 0)
  148.       mov (TIME2 + 3), (CTIME + 1)
  149.       setb TC2
  150.    1:
  151. reti
  152. DoCCF0:
  153.    clr CCF0
  154. DidCCF0:
  155.    djnz Sector1, 1f
  156.       mov Sector1, #SECTORS
  157.       mov (TIME1 + 0), CCAP0L
  158.       mov (TIME1 + 1), CCAP0H
  159.       mov (TIME1 + 2), (CTIME + 0)
  160.       mov (TIME1 + 3), (CTIME + 1)
  161.       setb TC1
  162.    1:
  163. reti
  164.  
  165. ;;; Pulse input scheduler
  166. ;;; When SECTORS (20) pulse inputs are counted from an input source, one full
  167. ;;; nutation is marked off, the time of that nutation is saved in the
  168. ;;; corresponding TIMEn variable, and the sector count is reset.  TCn marks this
  169. ;;; event.  At regular intervals a pending input is dequeued by looking for and
  170. ;;; resetting the first marked TCn flag.
  171. ;;; FXn is used to mark the first nutation in a test.  It is cleared thereafter.
  172. seg data at 0x21
  173. global TSTATUS: ds 1      ;;; Pulse-counter queue.
  174. global TC0 bit TSTATUS.0
  175. global TC1 bit TSTATUS.1
  176. global TC2 bit TSTATUS.2
  177. global TC3 bit TSTATUS.3
  178. global TC4 bit TSTATUS.4
  179. global TC5 bit TSTATUS.5
  180.  
  181. global State:   ds 1
  182. global FX0 bit State.0
  183. global FX1 bit State.1
  184. global FX2 bit State.2
  185. global FX3 bit State.3
  186. global FX4 bit State.4
  187. global FX5 bit State.5
  188. global FXX bit State.6
  189.  
  190. global Counter: ds 1
  191.  
  192. seg code
  193. PROCESS_RATE equ 2000
  194. global Scheduler:
  195.    mov TL0, #low(-PROCESS_RATE)
  196.    mov TH0, #high(-PROCESS_RATE)
  197.    clr TF0
  198.    setb TR0
  199.    setb ET0
  200. ret
  201. Process:             ;;; "Pause on TF0"
  202.    clr ET0
  203.    clr TR0
  204.    jbc TC0, ResumeX0  ;;; Dequeue input from pulse-counter queue.
  205.    jbc TC1, ResumeX1
  206.    jbc TC2, ResumeX2
  207.    jbc TC3, ResumeX3
  208.    jbc TC4, ResumeX4
  209.    jbc TC5, ResumeX5
  210. sjmp Scheduler
  211. FirstPartial:
  212.    acall Partial
  213. sjmp Scheduler
  214. Next:
  215.    acall Nutation
  216. sjmp Scheduler
  217. ResumeX0:
  218.    mov Counter, #0
  219.    jbc FX0, FirstPartial
  220. sjmp Next
  221. ResumeX1:
  222.    mov Counter, #1
  223.    jbc FX1, FirstPartial
  224. sjmp Next
  225. ResumeX2:
  226.    mov Counter, #2
  227.    jbc FX2, FirstPartial
  228. sjmp Next
  229. ResumeX3:
  230.    mov Counter, #3
  231.    jbc FX3, FirstPartial
  232. sjmp Next
  233. ResumeX4:
  234.    mov Counter, #4
  235.    jbc FX4, FirstPartial
  236. sjmp Next
  237. ResumeX5:
  238.    mov Counter, #5
  239.    jbc FX5, FirstPartial
  240. sjmp Next
  241.  
  242. Start: ;;; Install main(), set its return address to Idle().
  243.    mov HW, #Stack          ;;; HW = &Stack[0];
  244.    mov SP, #(SP_BASE - 1)  ;;; SP = SP_BASE - 1;
  245.    mov DPTR, #Exit
  246.    push DPL
  247.    push DPH                ;;; @SP++ = Exit();
  248.    acall main
  249. Idle:
  250.    orl PCON, #1
  251. sjmp Idle
  252.  
  253. global Spawn:            ;;; int Spawn(int @R0, void *(DPTR())) {
  254.    mov R1, HW
  255.    mov @R1, SP
  256.    inc HW                ;;;    @HW++ = SP;
  257.    dec R0
  258.    mov SP, R0            ;;;    SP = --R0;
  259.    acall Enter           ;;;    (*DPTR)();
  260. Exit:
  261.    dec HW
  262.    mov R0, HW
  263.    mov SP, @R0           ;;;    SP = @--HW;
  264. ret                      ;;; }
  265. Enter:
  266.    push DPL
  267.    push DPH
  268. ret
  269.  
  270. global Pause:              ;;; void Pause(int @@R0) {
  271.    mov @R0, SP             ;;;    @R0 = SP;
  272.    dec HW
  273.    mov R0, HW
  274.    mov SP, @R0             ;;;    SP = @--HW;
  275. ret                        ;;;    "idle until resume";
  276. global Resume:
  277.    mov R1, HW
  278.    mov @R1, SP
  279.    inc HW                  ;;;    @HW++ = SP;
  280.    mov SP, @R0             ;;;    SP = @R0;
  281. ret                        ;;; }
  282.